#include "SolverFun.h"
#include <set>
#include <map>
#include <functional>

#include "LGSolver.h"
#include "SWOSolver.h"
#include "DumpLog.h"
namespace Solver
{

SolverFun::SolverFun()
{

}

SolverFun::~SolverFun()
{

}

std::shared_ptr<BaseSolver> SolverFun::getSolver(SolverType_E type)
{
    auto mapSolver = getMapSolver();
    auto it = mapSolver.find(type);
    if (it == mapSolver.end())
    {
        LogError("当前求解器类型还未进行初始化实现, type = ", (int)type);
        return nullptr;
    }

    return it->second();
}

vecBaseSolver_t SolverFun::getSolvers(const vecSolverType_t types)
{
    //1.
    auto mapSolver = getMapSolver();
    std::set<SolverType_E> setType;
    vecBaseSolver_t vecSolver;
    for (auto tp : types)
    {
        //1.1
        auto it = mapSolver.find(tp);
        if (it == mapSolver.end())
        {
            LogError("当前求解器类型还未进行初始化实现, type = " + std::to_string((int)tp));
            continue;
        }

        //1.2
        setType.insert(tp);

        //1.3
        if (setType.size() != vecSolver.size())
        {
            vecSolver.push_back(it->second());
        }
    }

    //2.
    return vecSolver;
}

mapSolver_t SolverFun::getMapSolver()
{
    mapSolver_t mapSolver
    {
        {SolverType_E::LG_E, std::bind(initSolverByLG)},
        {SolverType_E::SWO_E, std::bind(initSolverBySWO)},
    };

    return mapSolver;
}

std::shared_ptr<BaseSolver> SolverFun::initSolverByLG()
{
    return std::make_shared<LGSolver>();
}

std::shared_ptr<BaseSolver> SolverFun::initSolverBySWO()
{
    return std::make_shared<SWOSolver>(false);
}

}